﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cklib;
using Silmoon.Threading;
using Cooky.Client.DomainDir;
using Cooky.Client.RecordDir;
using Silmoon.Windows.Controls.Extension;

namespace Cooky.Client.DomainDir
{
    public partial class DomainListForm : Silmoon.Windows.Forms.GenieForm
    {
        cklib.CookyDNSAPI cooky = null;
        Domain[] domainsCache = null;

        public DomainListForm(cklib.CookyDNSAPI cooky)
        {
            this.cooky = cooky;
            InitializeComponent();
            CloseStyle = WindowCloseStyle.MaxStyleExt;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            refreshList();
        }

        private void ctlModifyDomainButton_Click(object sender, EventArgs e)
        {
            modifyDomain();
        }
        private void ctlRefreshButton_Click(object sender, EventArgs e)
        {
            refreshList();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            panel1.Visible = false;
            base.OnFormClosing(e);
        }
        private void ctlExitButton_Click(object sender, EventArgs e)
        {
            Close();
        }
        void refreshList()
        {
            ctlRefreshButton.Enabled = false;
            ctlModifyDomainButton.Enabled = false;
            ctlEnableButton.Enabled = false;
            ctlDisableButton.Enabled = false;
            ctlDeleteButton.Enabled = false;

            Threads.ExecAsync(delegate()
            {
                DomainsResult result = cooky.GetDomains();
                this.Invoke(new EventHandler(delegate(object sender, EventArgs args)
                {
                    if (!result.Success) MessageBox.Show("错误：" + result.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else
                    {
                        domainsCache = result.Domains;
                        ctlDomainListView.BeginUpdate();
                        ctlDomainListView.Items.Clear();
                        foreach (Domain domain in domainsCache)
                        {
                            ListViewItem item = new ListViewItem(new string[] { domain.DomainName, domain.Records.ToString(), Translate.TranslateIt(domain.State), domain.ID.ToString() });
                            if (domain.State == "禁用") item.ForeColor = Color.Silver;
                            else if (domain.State == "正常") item.ForeColor = Color.Black;
                            else item.ForeColor = Color.Red;
                            ctlDomainListView.Items.Add(item);
                        }
                        ctlDomainListView.EndUpdate();
                    }
                    ctlRefreshButton.Enabled = true;
                }));
            });
        }
        void addDomain()
        {
            hideThis();
            DomainForm domainForm = new DomainForm(cooky);
            domainForm.FormClosed += new FormClosedEventHandler(Form_FormClosed);
            domainForm.ShowEx();
        }
        void modifyDomain()
        {
            string domain = ctlDomainListView.SelectedItems[0].Text;
            int domainID = int.Parse(ctlDomainListView.SelectedItems[0].SubItems[3].Text);

            hideThis();
            RecordListForm recordListForm = new RecordListForm();
            recordListForm.domainsCache = domainsCache;
            recordListForm.cooky = cooky;
            recordListForm.CurrentDomainID = domainID;
            recordListForm.CurrentDomain = domain;
            recordListForm.FormClosed += new FormClosedEventHandler(Form_FormClosed);
            recordListForm.ShowEx();
        }
        void deleteDomain()
        {
            if (ctlDomainListView.SelectedItems.Count == 0) return;
            string domain = ctlDomainListView.SelectedItems[0].Text;
            if (MessageBox.Show("确定删除" + ctlDomainListView.SelectedItems[0].Text + "？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                if (ctlDomainListView.SelectedItems.Count == 0) return;
                Threads.ExecAsync(delegate()
                {
                    cooky.RemoveDomain(domain);
                    this.Invoke(new tmpHander(delegate()
                    {
                        refreshList();
                    }));
                }, null);
            }
        }
        void disableDomain()
        {
            if (ctlDomainListView.SelectedItems.Count == 0) return;
            string domain = ctlDomainListView.SelectedItems[0].Text;
            if (MessageBox.Show("确定禁用" + ctlDomainListView.SelectedItems[0].Text + "？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                if (ctlDomainListView.SelectedItems.Count == 0) return;
                Threads.ExecAsync(delegate()
                {
                    cooky.SetDomainState(domain, false);
                    this.Invoke(new tmpHander(delegate()
                    {
                        refreshList();
                    }));
                }, null);
            }
        }
        void enableDomain()
        {
            if (ctlDomainListView.SelectedItems.Count == 0) return;
            string domain = ctlDomainListView.SelectedItems[0].Text;
            if (MessageBox.Show("确定启用" + ctlDomainListView.SelectedItems[0].Text + "？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                if (ctlDomainListView.SelectedItems.Count == 0) return;
                Threads.ExecAsync(delegate()
                {
                    cooky.SetDomainState(domain, true);
                    this.Invoke(new tmpHander(delegate()
                    {
                        refreshList();
                    }));
                }, null);
            }
        }
        void hideThis()
        {
            panel1.Visible = false;
            HideEx();
        }
        void Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!this.Visible)
            {
                panel1.Visible = true;
                this.ShowEx();
            }
            if (sender.GetType() == typeof(DomainForm))
            {
                DomainForm domainForm = sender as DomainForm;
                if (domainForm != null && domainForm.SucessState)
                {
                    refreshList();
                }
            }
        }

        private void ctlDomainListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ctlDomainListView.SelectedItems.Count == 0)
            {
                ctlModifyDomainButton.Enabled = false;
                ctlEnableButton.Enabled = false;
                ctlDisableButton.Enabled = false;
                ctlDeleteButton.Enabled = false;
            }
            else
            {
                ctlModifyDomainButton.Enabled = true;
                ctlEnableButton.Enabled = true;
                ctlDisableButton.Enabled = true;
                ctlDeleteButton.Enabled = true;
            }
        }

        private void ctlDomainListView_DoubleClick(object sender, EventArgs e)
        {
            if (ctlDomainListView.SelectedItems.Count != 0) modifyDomain();
        }

        private void ctlDeleteButton_Click(object sender, EventArgs e)
        {
            deleteDomain();
        }
        private void ctlDisableButton_Click(object sender, EventArgs e)
        {
            disableDomain();
        }
        private void ctlEnableButton_Click(object sender, EventArgs e)
        {
            enableDomain();
        }
        private void ctlAddDomainButton_Click(object sender, EventArgs e)
        {
            addDomain();
        }

        private void DomainListForm_Shown(object sender, EventArgs e)
        {
            FormGenieExtension.ShowControl(ctlAddDomainButton);
            FormGenieExtension.ShowControl(ctlRefreshButton, true, 200);
            FormGenieExtension.ShowControl(ctlModifyDomainButton, true, 400);
            FormGenieExtension.ShowControl(ctlDeleteButton, true, 600);
            FormGenieExtension.ShowControl(ctlDisableButton, true, 800);
            FormGenieExtension.ShowControl(ctlEnableButton, true, 1000);
            FormGenieExtension.ShowControl(ctlExitButton, true, 1200);
        }
    }
}
